package com.xy.controller.mq;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.tools.json.JSONUtil;
import com.xy.api.model.response.R;
import com.xy.controller.BaseController;
import com.xy.utils.TimeUtil;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/mq")
public class MqController extends BaseController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostMapping("/testDirectExchange")
    public R testDirectExchange() {
        String uuid = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("testDirectExchange", "testDirectExchange", uuid);
        return success();
    }

    @PostMapping("/testTopicExchange")
    public R testTopicExchange(@RequestParam String routingKey) {
        String uuid = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("testTopicExchange", routingKey, uuid);
        return success();
    }

    @PostMapping("/testDlx")
    public R testDlx(@RequestParam int delayInMills) {
        String uuid = UUID.randomUUID().toString();

        MessagePostProcessor processor = msg -> {
            msg.getMessageProperties().setExpiration(String.valueOf(delayInMills));
            return msg;
        };


        rabbitTemplate.convertAndSend("delayQueue", (Object) uuid, processor);


        return success();
    }
}
